package com.examsys.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.examsys.dao.PaperDao;
import com.examsys.dao.PaperDetailDao;
import com.examsys.dao.PaperSectionDao;
import com.examsys.po.Paper;
import com.examsys.po.PaperSection;
/**
 * 试卷业务逻辑层实现类
 * @author edu-1
 *
 */
@Service("paperService")
public class PaperServiceImpl extends AbstractBaseService<Paper, Integer> implements PaperService {
	@Resource
	private PaperDao dao;//试卷数据访问层对象
	@Resource
	private PaperSectionDao paperSectionDao;//试卷章节数据访问层对象
	@Resource
	private PaperDetailDao paperDetailDao;//试卷明细数据访问层对象
	
	public PaperDao getDao() {
		return dao;
	}

	public void setDao(PaperDao dao) {
		this.dao = dao;
	}

	public PaperSectionDao getPaperSectionDao() {
		return paperSectionDao;
	}

	public void setPaperSectionDao(PaperSectionDao paperSectionDao) {
		this.paperSectionDao = paperSectionDao;
	}

	public PaperDetailDao getPaperDetailDao() {
		return paperDetailDao;
	}

	public void setPaperDetailDao(PaperDetailDao paperDetailDao) {
		this.paperDetailDao = paperDetailDao;
	}

	/**
	 * 添加试卷
	 *  
	 */
	@Override
	public boolean add(Paper obj) throws Exception{
		boolean flag=false;
		Integer id = this.getDao().getSeq();
		obj.setId(id);
		
		this.getDao().add(obj);//先保存试卷对象
		
		//再保存章节的对象
		List<PaperSection> paperSections = obj.getPaperSections();
		for(PaperSection ps:paperSections){
			ps.setPaper(obj);//关联试卷对象
			this.paperSectionDao.add(ps);//保存章节对象
		}
		flag=true;
		return flag;
	}

	/**
	 * 修改试卷
	 */
	@Override
	public boolean update(Paper obj) throws Exception{
		boolean flag=false;
		this.getDao().update(obj);
		List<PaperSection> paperSections = obj.getPaperSections();
		for(PaperSection paperSection:paperSections){
			paperSection.setPaper(obj);
			if(paperSection.getId()==null){//如果此章节信息是新添加的
				this.getPaperSectionDao().add(paperSection);
			}else{//如果此章节信息是新旧有的
				this.getPaperSectionDao().update(paperSection);
			}
		}
		flag=true;
		return flag;
	}

	/**
	 * 通过试卷编号删除试卷
	 * @param id 编号
	 */
	@Override
	public boolean delete(Integer id) throws Exception{
		boolean flag=false;
		this.getPaperDetailDao().deleteByPaperId(id);//先删除试卷中的试题
		this.getPaperSectionDao().deleteByPaperId(id);//再删除试卷章节
		this.getDao().delete(id);//再删除试卷本身
		flag=true;
		return flag;
	}

	/**
	 * 获取试卷
	 * @param id 编号
	 */
	@Override
	public Paper get(Integer id) throws Exception{
		Paper paper=null;
		paper = this.getDao().get(id);
		return paper;
	}

	/**
	 * 获得试卷表所有记录
	 */
	@Override
	public List<Paper> getList() throws Exception{
		List<Paper> list=null;
		list = this.getDao().getList1();
		
		return list;
	}

	/**
	 * 带条件获取试卷记录
	 */
	@Override
	public List<Paper> getList(Paper obj) throws Exception{
		List<Paper> list=null;
		list = this.getDao().getList2(obj);
		
		return list;
	}

}
